package cn.cntv.app.ipanda.home.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;

import cn.cntv.app.ipanda.R;
import cn.cntv.app.ipanda.home.entity.Data;
import cn.cntv.app.ipanda.home.entity.GroupData;
import cn.cntv.app.ipanda.home.entity.ItemData1;
import cn.cntv.app.ipanda.home.entity.ItemData2;
import cn.cntv.app.ipanda.home.entity.ItemData3;
import cn.cntv.app.ipanda.utils.AutoUtils;
import cn.cntv.app.ipanda.utils.ViewHolder;

/**
 * @ClassName: HomeAdapter
 * @author Xiao JinLai
 * @Date Dec 23, 2015 5:57:13 PM
 * @Description：
 */
public class HomeAdapter extends BaseAdapter {

	private int typeCount;
	private ArrayList<Data> mDatas;
	private LayoutInflater mInflater;
	private TextView nameTextView, infoTextView;
	private ImageView mImageView, mGridImageView1, mGridImageView2,
			mGridImageView3;
	private View.OnClickListener mClickListener;

	public HomeAdapter(LayoutInflater inflater, int typeCount,
			ArrayList<Data> datas, View.OnClickListener onclick) {
		this.mInflater = inflater;
		this.typeCount = typeCount <= 0 ? 1 : typeCount;
		this.mDatas = datas;
		this.mClickListener = onclick;
	}

	private void upDate(int typeCount, ArrayList<Data> datas) {
		this.typeCount = typeCount <= 0 ? 1 : typeCount;
		if (this.mDatas != null) {
			this.mDatas.clear();
			this.mDatas.addAll(datas);
		}
		this.mDatas = datas;
		notifyDataSetChanged();
	}

	@Override
	public int getItemViewType(int position) {
		return mDatas.get(position).mType;
	}

	@Override
	public int getViewTypeCount() {
		return typeCount;
	}

	public Data getItemData(int pos, int index) {
		if (mDatas == null || mDatas.isEmpty()) {
			return null;
		}
		Data item = mDatas.get(pos);
		if (item.mType == 2) {
			ItemData2 itemData = (ItemData2) item;
			if (itemData.itemData == null || itemData.itemData.isEmpty()) {
				return null;
			} else {
				return itemData.itemData.get(index);
			}
		}
		return null;
	}

	@Override
	public boolean isEnabled(int position) {

		if (mDatas.get(position).mType == 0 || // 分组标题
				mDatas.get(position).mType == 2)// 九宫格样式
		{
			return false;
		}

		return super.isEnabled(position);
	}

	@Override
	public int getCount() {
		return mDatas == null ? 0 : mDatas.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (getItemViewType(position) == 0)// group name
		{
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.item_group, parent,
						false);
				AutoUtils.autoSize(convertView);
			}
			nameTextView = ViewHolder.get(convertView,
					R.id.name_TV);
			GroupData data = (GroupData) mDatas.get(position);
			nameTextView.setText(data.name);
		} else if (getItemViewType(position) == 1) {
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.item_1, parent, false);
				AutoUtils.autoSize(convertView);
			}
			mImageView = (ImageView) ViewHolder.get(convertView,
					R.id.icon_IV);
			nameTextView = ViewHolder.get(convertView,
					R.id.name_TV);
			ItemData1 data = (ItemData1) mDatas.get(position);
			nameTextView.setText(data.name);
		} else if (getItemViewType(position) == 2)//
		{
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.item_2, parent, false);
				AutoUtils.autoSize(convertView);
			}
			View view1 = ViewHolder
					.get(convertView, R.id.grid1RL);
			view1.setTag(position + "," + 0);
			view1.setOnClickListener(mClickListener);

			View view2 = ViewHolder
					.get(convertView, R.id.grid2RL);
			view2.setTag(position + "," + 1);
			view2.setOnClickListener(mClickListener);

			ItemData2 data = (ItemData2) mDatas.get(position);
			mGridImageView1 = (ImageView) ViewHolder.get(
					convertView, R.id.grid1IV);
			mGridImageView1 = (ImageView) ViewHolder.get(
					convertView, R.id.grid2IV);
			nameTextView = ViewHolder.get(convertView,
					R.id.grid1NameTV);
			infoTextView = ViewHolder.get(convertView,
					R.id.grid1InfoTV);
			nameTextView.setText(data.itemData.get(0).name);
			infoTextView.setText(data.itemData.get(0).info);
			nameTextView = ViewHolder.get(convertView,
					R.id.grid2NameTV);
			infoTextView = ViewHolder.get(convertView,
					R.id.grid2InfoTV);
			nameTextView.setText(data.itemData.get(1).name);
			infoTextView.setText(data.itemData.get(1).info);

		} else if (getItemViewType(position) == 3)//
		{
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.item_3, parent, false);
				AutoUtils.autoSize(convertView);
			}
			mGridImageView1 = (ImageView) ViewHolder.get(
					convertView, R.id.grid1IV);
			mGridImageView1 = (ImageView) ViewHolder.get(
					convertView, R.id.grid2IV);
			nameTextView = ViewHolder.get(convertView,
					R.id.grid1NameTV);
			infoTextView = ViewHolder.get(convertView,
					R.id.grid1InfoTV);

			ItemData3 data = (ItemData3) mDatas.get(position);
			nameTextView.setText(data.name);
			infoTextView.setText(data.info);

		}

		return convertView;
	}
}
